package cn.myra.Date;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Year;
import java.util.Calendar;
import java.util.Date;

public class TestDate {

	
	public static void main(String[] args) {
		//日期字符串互转
		//test02();
		//calender 类应用
		test03();
	}
	
	

	private static void test03() {
		//日历类
		Calendar calendar = Calendar.getInstance();
		
		Date date = calendar.getTime();
		
		System.out.println(date);
		System.out.println(calendar.getFirstDayOfWeek());
		System.out.println(calendar.get(Calendar.WEEK_OF_YEAR));
		int dayn = calendar.get(calendar.DAY_OF_WEEK_IN_MONTH);
		System.out.println(dayn);
		System.out.println(calendar.get(Calendar.MONTH)+1);
		
		
	}
	
	
	
	
	

	private static void test02() {
		// 日期对象
		Date date = new Date(2018, 2, 28);
		Date date2 = new Date(999999999999L);
		Date date3 = new Date();
		System.out.println(date);
		System.out.println(date2);
		System.out.println(date3);
		
		//
		
		String string2 = date2string(date2, "yyyy-MM-dd");
		System.out.println(string2);
		date3 = string2date("2017年9月15日", "yyyy年MM月dd日");
		System.out.println(date3);
				

	}
	
	
	private static Date string2date(String str,String strFormat) {
		//结果日期
		Date date = null;
		
		if(strFormat==null||strFormat=="") {
			strFormat = "yyyy年MM月dd日";
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat(strFormat);
		
		
		if(str!=null && str.length()>0) {
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		return date;
	}
	
	
	

	private static String date2string(Date date, String dateFormat) {
		// 结果字符串
		String str;
		// 判断date是否为空
		if (date == null) {
			date = new Date();
		}
		if(dateFormat==null||dateFormat=="") {
			dateFormat = "当前时间：yyyy-MM-dd HH:mm:ss";
		}
		
		//simpleDateFormat
		SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
		str = sdf.format(date);
		
		return str;

	}

}
